package com.data;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.cards.Motor;

public class motorTable extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int    DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME    = "db_motor";

    // Contacts table name
    private static final String TABLE_MOTOR      = "tbl_motor";

    // Contacts Table Columns names
    private static final String ID               = "id";
    private static final String CYLINDER         = "cylinder";
    private static final String POWER            = "power";
    private static final String GEAR             = "gear";
    private static final String WEIGHT           = "weight";
    private static final String SPEED            = "speed";



    /**
     * Constructor
     * 
     * @param context to use to open or create the database
     * */
    public motorTable(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    // Creating Tables

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_MOTOR_TABLE = TABLE_MOTOR + "("
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + CYLINDER + " INTEGER," + POWER + " INTEGER, " + GEAR + " INTEGER," + WEIGHT + " INTEGER,"
                + SPEED + " INTEGER" + ");";
        db.execSQL("CREATE TABLE IF NOT EXISTS " + CREATE_MOTOR_TABLE);
    }



    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOTOR);

        // Create tables again
        onCreate(db);
    }



    public long addMotor(Motor motor) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(CYLINDER, motor.getCylinder());
        values.put(POWER, motor.getPower());
        values.put(GEAR, motor.getGear());
        values.put(WEIGHT, motor.getWeight());
        values.put(SPEED, motor.getSpeed());

        // Inserting Row
        long result = db.insert(TABLE_MOTOR, null, values);
        db.close(); // Closing database connection
        return result;
    }



    Motor getMotor(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_MOTOR, new String[]{ ID, CYLINDER, POWER,
                GEAR, WEIGHT, SPEED, }, ID + "=?",
                new String[]{ String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Motor motor = new Motor(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getInt(3),
                cursor.getInt(4), cursor.getInt(5));

        db.close();
        cursor.close();

        // return motor
        return motor;
    }



    public List<Motor> getAllMotors() {
        List<Motor> motorList = new ArrayList<Motor>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_MOTOR;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                Motor motor = new Motor(cursor.getInt(0), cursor.getInt(1),
                        cursor.getInt(2), cursor.getInt(3),
                        cursor.getInt(4), cursor.getInt(5));

                // Adding motor to list
                motorList.add(motor);
            }
            while (cursor.moveToNext());
        }

        db.close();
        cursor.close();

        // return motorList
        return motorList;
    }



    public int updateMotor(Motor motor) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(CYLINDER, motor.getCylinder());
        values.put(POWER, motor.getPower());
        values.put(GEAR, motor.getGear());
        values.put(WEIGHT, motor.getWeight());
        values.put(SPEED, motor.getSpeed());

        // updating row
        int result = db.update(TABLE_MOTOR, values, ID + " = ?",
                new String[]{ String.valueOf(motor.getId()) });
        db.close();
        return result;
    }



    public int deleteContact(Motor motor) {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete(TABLE_MOTOR, ID + " = ?",
                new String[]{ String.valueOf(motor.getId()) });
        db.close();
        return result;
    }



    public int getNumberOfMotors() {
        String countQuery = "SELECT  * FROM " + TABLE_MOTOR;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int result = cursor.getCount();

        db.close();
        cursor.close();

        return result;
    }

}